<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="3">
        <el-select
          v-model="search.request_method"
          filterable
          clearable
          placeholder="请选择请求类型">
          <el-option
            v-for="item in search.request_method_options"
            :key="item.id"
            :label="item.request_method"
            :value="item.request_method">
          </el-option>
      </el-select>
      </el-col>
      <el-col :span="6">
        <el-input
          v-model="search.name"
          placeholder="请输入mock名称"
          prefix-icon="el-icon-search"
          @keyup.enter.native="SearchMock()">
        </el-input>
      </el-col>
      <el-col :span="3">
        <el-button
            type="primary"
            icon="el-icon-search"
            round
            @click="SearchMock()">搜索</el-button>
      </el-col>
    </el-row>
    <br>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" min-width="5%">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="mock名称" min-width="12%" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link @click="edit(scope.row.id)" style="color:#298abd">{{scope.row.name == '' ? '-' : scope.row.name}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="请求方式" min-width="6%" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.request_method }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请求地址" min-width="20%" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.mock_url }}
        </template>
      </el-table-column>
      <el-table-column label="编写人员" min-width="10%" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="更新时间" min-width="12%" align="center">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ $common.formatDateTime(scope.row.update_time) }}</span>
          <span></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="15%" align="center">
        <template slot-scope="scope">
          <el-button-group>
         <el-tooltip content="测试" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="success"
                icon="el-icon-position"
                @click="run(scope.row.id)">
              </el-button>
            </el-tooltip>
            <el-tooltip content="编辑" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="edit(scope.row.id)">
              </el-button>
            </el-tooltip>
            <el-tooltip content="复制" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="warning"
                icon="el-icon-copy-document"
                @click="copy(scope.row.id)">
              </el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="del(scope.row.id)">
              </el-button>
            </el-tooltip>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="测试mock接口"
      :visible.sync="dialogVisible"
      width="30%"
      >
      <span>响应结果为 : {{ mockResult }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="1"
      :page-sizes="[10, 20]"
      :page-size="pagination.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total">
    </el-pagination>
  </div>
</template>

<script>
import { getMockList, deleteMock, testMock } from '@/api/mock/table'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      search: {
        request_method: '',
        request_method_options: [
          { 'id': 0, 'request_method': 'GET' },
          { 'id': 1, 'request_method': 'POST' }
        ],
        mock_name: ''
      },
      pagination: {
        total: 0,
        page_current: 1,
        page_num: 1,
        page_size: 10
      },
      mockResult: '',
      list: null,
      ispagination: false,
      listLoading: true,
      dialogVisible: false
    }
  },
  created() {
    this.SearchMock()
  },
  methods: {
    SearchMock: function() {
      var params = {}

      if (this.search.request_method) {
        params['request_method'] = this.search.request_method
      }

      if (this.search.name) {
        params['search'] = this.search.name
      }

      if (this.ispagination) {
        params['page_num'] = this.pagination.page_num
        params['page_size'] = this.pagination.page_size
      } else {
        params['page_num'] = 1
        params['page_size'] = this.pagination.page_size
      }
      params['ordering'] = '-update_time'

      this.listLoading = true
      getMockList(params).then(response => {
        this.list = response.results
        this.listLoading = false
        this.pagination.total = response.count
      })
      if (this.ispagination === false) {
        this.pagination.page_num = 1
      }
      this.ispagination = false
    },

    run(id) {
      var data = {}
      data['id'] = id
      testMock(data).then(response => {
        this.mockResult = response.data
        this.dialogVisible = true
      })
    },

    handleSizeChange: function(val) {
      this.pagination.page_size = val
      this.SearchMock()
    },
    handleCurrentChange: function(val) {
      this.ispagination = true
      this.pagination.page_num = val
      this.SearchMock()
    },

    edit(arg) {
      this.$router.push({
        path: '/mock/update_mock',
        query: {
          id: arg,
          operation_type: 1
        }
      })
    },

    copy(arg) {
      this.$router.push({
        path: '/mock/create_mock',
        query: {
          id: arg,
          operation_type: 2
        }
      })
    },

    del(arg) {
      this.$confirm('此操作将删除该Mock接口, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {}
        params['id'] = arg
        deleteMock(params).then(response => {
          if (response.status === '000000') return
          this.$message.success('删除成功')
          this.SearchMock()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
